<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>OpenFeign微服务调用测试</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
            background-color: #fafafa;
        }
        .section h2 {
            color: #2c3e50;
            margin-top: 0;
        }
        .button-group {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin-bottom: 20px;
        }
        button {
            padding: 10px 20px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #2980b9;
        }
        button.test-btn {
            background-color: #e74c3c;
        }
        button.test-btn:hover {
            background-color: #c0392b;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            background-color: #ecf0f1;
            border-radius: 5px;
            border-left: 4px solid #3498db;
            white-space: pre-wrap;
            font-family: 'Courier New', monospace;
            font-size: 12px;
            max-height: 400px;
            overflow-y: auto;
        }
        .error {
            border-left-color: #e74c3c;
            background-color: #fdf2f2;
            color: #c0392b;
        }
        .success {
            border-left-color: #27ae60;
            background-color: #f2fdf2;
            color: #27ae60;
        }
        .input-group {
            margin-bottom: 15px;
        }
        .input-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        .input-group input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🚀 OpenFeign微服务调用测试平台</h1>
        
        <!-- 促销服务测试 -->
        <div class="section">
            <h2>📊 促销服务 (PromotionService)</h2>
            <div class="button-group">
                <button onclick="callFeignAPI('/feign/promotions', 'promotionResult')">获取所有促销</button>
                <button onclick="callFeignAPI('/feign/promotions/active', 'promotionResult')">获取活跃促销</button>
                <button onclick="callFeignAPI('/feign/promotions/1', 'promotionResult')">获取促销ID=1</button>
            </div>
            <div id="promotionResult" class="result">点击上方按钮测试促销服务...</div>
        </div>

        <!-- 生鲜商品服务测试 -->
        <div class="section">
            <h2>🥬 生鲜商品服务 (FreshProductService)</h2>
            <div class="button-group">
                <button onclick="callFeignAPI('/feign/fresh-products', 'productResult')">获取所有商品</button>
                <button onclick="callFeignAPI('/feign/fresh-products/promotions', 'productResult')">获取促销商品</button>
                <button onclick="callFeignAPI('/feign/fresh-products/category/蔬菜', 'productResult')">获取蔬菜类商品</button>
                <button onclick="callFeignAPI('/feign/fresh-products/category/水果', 'productResult')">获取水果类商品</button>
            </div>
            <div id="productResult" class="result">点击上方按钮测试生鲜商品服务...</div>
        </div>

        <!-- 负载均衡测试 -->
        <div class="section">
            <h2>⚖️ 负载均衡测试 (LoadBalancer)</h2>
            <div class="button-group">
                <button onclick="callFeignAPI('/feign/loadbalancer/current-instance', 'loadbalancerResult')">获取当前实例</button>
                <button onclick="callFeignAPI('/feign/loadbalancer/stats', 'loadbalancerResult')">获取统计信息</button>
                <button class="test-btn" onclick="testLoadBalance(5)">测试负载均衡(5次)</button>
                <button class="test-btn" onclick="testLoadBalance(10)">测试负载均衡(10次)</button>
                <button class="test-btn" onclick="testLoadBalance(20)">测试负载均衡(20次)</button>
            </div>
            <div id="loadbalancerResult" class="result">点击上方按钮测试负载均衡...</div>
        </div>

        <!-- 批量测试 -->
        <div class="section">
            <h2>🔄 批量测试</h2>
            <div class="input-group">
                <label for="testTimes">测试次数:</label>
                <input type="number" id="testTimes" value="10" min="1" max="100">
            </div>
            <div class="button-group">
                <button class="test-btn" onclick="batchTest()">开始批量测试</button>
                <button onclick="clearAllResults()">清空所有结果</button>
            </div>
            <div id="batchResult" class="result">配置测试次数后点击开始批量测试...</div>
        </div>
    </div>

    <script>
        // 调用Feign API
        async function callFeignAPI(url, resultElementId) {
            const resultElement = document.getElementById(resultElementId);
            resultElement.textContent = '正在调用API...';
            resultElement.className = 'result';
            
            try {
                const response = await fetch(url);
                const data = await response.json();
                
                if (response.ok) {
                    resultElement.textContent = JSON.stringify(data, null, 2);
                    resultElement.className = 'result success';
                } else {
                    resultElement.textContent = `错误 ${response.status}: ${JSON.stringify(data, null, 2)}`;
                    resultElement.className = 'result error';
                }
            } catch (error) {
                resultElement.textContent = `网络错误: ${error.message}`;
                resultElement.className = 'result error';
            }
        }

        // 测试负载均衡
        async function testLoadBalance(times) {
            const resultElement = document.getElementById('loadbalancerResult');
            resultElement.textContent = `正在进行${times}次负载均衡测试...`;
            resultElement.className = 'result';
            
            try {
                const response = await fetch(`/feign/test/load-balance?times=${times}`);
                const data = await response.json();
                
                if (response.ok) {
                    resultElement.textContent = JSON.stringify(data, null, 2);
                    resultElement.className = 'result success';
                } else {
                    resultElement.textContent = `错误 ${response.status}: ${JSON.stringify(data, null, 2)}`;
                    resultElement.className = 'result error';
                }
            } catch (error) {
                resultElement.textContent = `网络错误: ${error.message}`;
                resultElement.className = 'result error';
            }
        }

        // 批量测试
        async function batchTest() {
            const times = document.getElementById('testTimes').value;
            const resultElement = document.getElementById('batchResult');
            
            resultElement.textContent = `开始批量测试 ${times} 次...`;
            resultElement.className = 'result';
            
            const results = [];
            
            for (let i = 0; i < times; i++) {
                try {
                    const response = await fetch('/feign/loadbalancer/current-instance');
                    const data = await response.json();
                    results.push({
                        test: i + 1,
                        success: response.ok,
                        data: data
                    });
                    
                    resultElement.textContent = `完成 ${i + 1}/${times} 次测试...`;
                } catch (error) {
                    results.push({
                        test: i + 1,
                        success: false,
                        error: error.message
                    });
                }
            }
            
            // 统计结果
            const successCount = results.filter(r => r.success).length;
            const summary = {
                totalTests: times,
                successCount: successCount,
                failureCount: times - successCount,
                successRate: ((successCount / times) * 100).toFixed(2) + '%',
                results: results
            };
            
            resultElement.textContent = JSON.stringify(summary, null, 2);
            resultElement.className = successCount === times ? 'result success' : 'result error';
        }

        // 清空所有结果
        function clearAllResults() {
            const resultElements = document.querySelectorAll('.result');
            resultElements.forEach(element => {
                element.textContent = '结果已清空...';
                element.className = 'result';
            });
        }
    </script>
</body>
</html>
